package com.chuangmei.com.arindoorlocationsystem.home;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.VideoView;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.ar.ARStudyActivity;
import com.chuangmei.com.arindoorlocationsystem.base.BaseActivity;
import com.chuangmei.com.arindoorlocationsystem.config.ConfigMessageActivity;
import com.chuangmei.com.arindoorlocationsystem.map.IndoorPositionActivity;
import com.chuangmei.com.arindoorlocationsystem.utils.Constant;
import com.chuangmei.com.arindoorlocationsystem.video.VideoPlayActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeActivity extends BaseActivity {

    @BindView(R.id.mVV_visit)
    VideoView mVV_visit;
    @BindView(R.id.mVV_study)
    VideoView mVV_study;
    private int mVV_visit_position = 0;
    private int mVV_study_position = 0;
    private boolean first_load = true;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_home);
    }

    @Override
    protected void initView() {
        mVV_visit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (mVV_visit.isPlaying() || !TextUtils.isEmpty(Constant.test_video_path_test1)) {
                    //                    jumpFragment(Constant.test_video_path_test1, mVV_visit.getCurrentPosition());
                    Intent intent = new Intent(getApplicationContext(), VideoPlayActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("video_uri", Constant.test_video_path_test1);
                    bundle.putInt("video_play_position", mVV_visit.getCurrentPosition());
                    bundle.putInt("video_screen_orientation", ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    intent.putExtras(bundle);
                    mVV_visit.pause();
                    mVV_study.pause();
                    startActivity(intent);
                }
                return false;
            }
        });
        mVV_visit.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (!first_load) {
                    mp.start();
                    mp.setLooping(true);
                }
            }
        });


        mVV_study.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (mVV_study.isPlaying() || !TextUtils.isEmpty(Constant.test_video_path_test2)) {
                    //                    jumpFragment(Constant.test_video_path_test1, mVV_visit.getCurrentPosition());
                    Intent intent = new Intent(getApplicationContext(), VideoPlayActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("video_uri", Constant.test_video_path_test2);
                    bundle.putInt("video_play_position", mVV_study.getCurrentPosition());
                    bundle.putInt("video_screen_orientation", ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    intent.putExtras(bundle);
                    mVV_visit.pause();
                    mVV_study.pause();
                    startActivity(intent);
                }
                return false;
            }
        });
        
        mVV_study.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (!first_load) {
                    mp.start();
                    mp.setLooping(true);
                }
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
        mVV_visit.pause();
        mVV_study.pause();
        mVV_visit_position = mVV_visit.getCurrentPosition();
        mVV_study_position = mVV_study.getCurrentPosition();
        first_load = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(Constant.test_video_path_test1)) {
            mVV_visit.setVideoURI(Uri.parse(Constant.test_video_path_test1));
            mVV_visit.seekTo(mVV_visit_position);
            mVV_visit.start();
        }

        if (!TextUtils.isEmpty(Constant.test_video_path_test2)) {
            mVV_study.setVideoURI(Uri.parse(Constant.test_video_path_test2));
            mVV_study.seekTo(mVV_study_position);
            mVV_study.start();
        }
        first_load = false;
        mVV_visit_position = 0;
        mVV_study_position = 0;
    }

    @OnClick({R.id.mIV_home_gear, R.id.mBT_visit, R.id.mBT_study})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.mIV_home_gear:
                intent = new Intent();
                intent.setClass(this, ConfigMessageActivity.class);
                break;
            case R.id.mBT_visit:
                intent = new Intent();
                intent.setClass(this, IndoorPositionActivity.class);
                break;
            case R.id.mBT_study:
                intent = new Intent();
                intent.setClass(this, ARStudyActivity.class);
                break;

        }
        if (intent != null) {
            startActivity(intent);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
